/*
 * Copyright 2011 Shotaro Uchida <fantom@xmaker.mx>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jazzkaffe;

import java.net.ProtocolException;

/**
 * ZigBee 64bit IEEE Address
 * @author Shotaro Uchida <fantom@xmaker.mx>
 */
public class IEEEAddress extends ZigBeeAddress {
    
    public static final int ADDRESS_SIZE = 8;
    public static final IEEEAddress NULL_ADDRESS = new IEEEAddress(new byte[] {
        (byte) 0x00,
        (byte) 0x00,
        (byte) 0x00,
        (byte) 0x00,
        (byte) 0x00,
        (byte) 0x00,
        (byte) 0x00,
        (byte) 0x00
    });
    
    protected IEEEAddress(byte[] addr64) {
        super(addr64, ADDRESS_SIZE);
    }
    
    protected IEEEAddress(String address) throws ProtocolException {
        super(address, ADDRESS_SIZE);
    }

    public static IEEEAddress getByAddress(byte[] addr64) {
        return new IEEEAddress(addr64);
    }
    
    public static IEEEAddress getByAddress(String str)throws ProtocolException {
        return new IEEEAddress(str);
    }
    
    public static IEEEAddress getByAddress(long address) {
        byte[] addr64 = new byte[ADDRESS_SIZE];
        for (int p = 0; p < ADDRESS_SIZE; p++) {
            addr64[p] = (byte) ((address >> (8 * (ADDRESS_SIZE - 1 - p))) & 0xffL);
        } 
        return new IEEEAddress(addr64);
    }
    
    public long toLong() {
        long address = 0;
        for (int p = 0; p < ADDRESS_SIZE; p++) {
            long d = mac[p] & 0xff;
            address |= (d << (8 * (ADDRESS_SIZE - 1 - p)));
        } 
        return address;
    }
}
